package com.clss.commandhelper.function.homefragment2nd;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.clss.commandhelper.R;
import com.clss.commandhelper.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuv
 * @date 2017/12/5
 * 线索报告
 */

public class Home2ndFragment extends BaseFragment implements Home2ndPresenter.Home2ndView,
        AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{
    private static final String TAG = "Home2ndFragment";

    private Home2ndPresenter mHome2ndPresenter;
    private SwipeRefreshLayout mHome2ndSrl;
    private ListView mHome2ndLv;
    private AlarmAdapter mAlarmAdapter;
    private List<ClueEntity> mAlarmList = new ArrayList<>();

    @Override
    protected int setViewId() {
        return R.layout.fragment_home4th;
    }

    @Override
    protected void initView(View v) {
        mHome2ndSrl = v.findViewById(R.id.home_4th_srl);
        mHome2ndLv = v.findViewById(R.id.home_4th_lv);
        mHome2ndSrl.setOnRefreshListener(this);

        mHome2ndPresenter = new Home2ndPresenter();
        mHome2ndPresenter.setHome4thView(this);

        mAlarmAdapter = new AlarmAdapter(getContext(), 0);
        mHome2ndLv.setAdapter(mAlarmAdapter);
        mHome2ndLv.setOnItemClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        getList();
    }

    private void getList(){
        if (!mHome2ndSrl.isRefreshing()){
            mHome2ndSrl.setRefreshing(true);
        }
        mHome2ndPresenter.getClueList();
    }

    private void stopRefreshing(){
        mHome2ndSrl.setRefreshing(false);
    }

    @Override
    public void requestFailure(Throwable t) {
        stopRefreshing();
    }

    @Override
    public void responseError(int code, String msg) {
        stopRefreshing();
        showShortToast(msg);
    }

    @Override
    public void getClueListSuccess(List<ClueEntity> alarmEntityList) {
        mHome2ndSrl.setRefreshing(false);
        mAlarmList.clear();
        mAlarmList.addAll(alarmEntityList);

        mAlarmAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        getList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Bundle bundle = new Bundle();
//        bundle.putString(CaseDetailActivity.TASK_ID, mAlarmList.get(position).getPoliceInfoTaskId());
//        bundle.putString(CaseDetailActivity.ID, mAlarmList.get(position).getId());
//        bundle.putInt(CaseDetailActivity.DISPATCH_STATUS, mAlarmList.get(position).getDispatchStatus());
//        bundle.putString(CaseDetailActivity.AREA_CODE, mAlarmList.get(position).getAreaCode());
//        Utils.startActivity(getActivity(), CaseDetailActivity.class, bundle);
    }
    
    class AlarmAdapter extends ArrayAdapter {
        public AlarmAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @Override
        public int getCount() {
            return mAlarmList.size();
        }

        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ClueEntity clueEntity = mAlarmList.get(position);
            View v = getLayoutInflater().inflate(R.layout.item_task_list, null);
            TextView timeTv = v.findViewById(R.id.task_item_time);
            TextView addressTv = v.findViewById(R.id.task_item_address);
            TextView nameTv = v.findViewById(R.id.task_item_name);
            TextView statusTv = v.findViewById(R.id.task_item_state);
//            if (liveVideoTaskEntity.getDispatchStatus() == 1){
//                statusTv.setText("未认领");
//            }else if (liveVideoTaskEntity.getDispatchStatus() == 2){
//                statusTv.setText("正在处理");
//            }else if (liveVideoTaskEntity.getDispatchStatus() == 3){
//                statusTv.setText("已完成");
//            }
//            timeTv.setText(liveVideoTaskEntity.getCreateTime());
//            addressTv.setText(liveVideoTaskEntity.getPoliceInfoAddress());
//            if (liveVideoTaskEntity.getTaskName() != null){
//                nameTv.setText(liveVideoTaskEntity.getTaskName());
//            }
            return v;
        }
    }
}
